<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>手机号验证登录</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  <script type="text/javascript" src="js/jquery-1.8.3.js"></script>
  <script>
  var phonenumber="";   //手机号
  var vertification="";  //用户输入的验证码
  //发送验证码时添加cookie
function addCookie(name,value,expiresHours){ 
    var cookieString=name+"="+escape(value); //获取剩余的秒数
    //判断是否设置过期时间,0代表关闭浏览器时失效
    if(expiresHours>0){ 
        var date=new Date(); 
        date.setTime(date.getTime()+expiresHours*1000); 
        cookieString=cookieString+";expires=" + date.toUTCString(); 
    } 
  //   alert(cookieString);
        document.cookie=cookieString; 
} 
//修改cookie的值
function editCookie(name,value,expiresHours){ 
    var cookieString=name+"="+escape(value); 
    if(expiresHours>0){ 
      var date=new Date(); 
      date.setTime(date.getTime()+expiresHours*1000); //单位是毫秒
      cookieString=cookieString+";expires=" + date.toGMTString(); 
    } 
      document.cookie=cookieString; 
} 
//根据名字获取cookie的值
function getCookieValue(name){ 
      var strCookie=document.cookie; //getcookie
  //    alert("strCookie  "+strCookie)
      var arrCookie=strCookie.split("="); //"="
      return unescape(arrCookie[2]); //第三个
   //   alert("result i think "+arrCookie[2]);
 /*     for(var i=0;i<arrCookie.length;i++){ 
        var arr=arrCookie[i].split("="); 
        if(arr[0]==name){  
          return unescape(arr[1]);
          break;
        }else{
             return ""; 
             break;
         } 
      } 
  */     
}

//发送验证码
function sendCode(){
 $.ajax({
type: "POST",
url:"vertify.action",	//把表单数据发送到ajax.jsp
dataType: "json", 	//返回数据类型
data:{
"phonenumber":phonenumber.value
},
async: true,
error: function(request) {
alert("发送请求失败！");
},
success: function(data) {
var json = eval("(" + data + ")"); 
if(json.respCode=="105122"){
alert("验证码发送次数超限，请明天再试");
//addCookie("secondsremained",60,60);//添加cookie记录,有效时间60s
//settime($("#getvertification"));//开始倒计时
}
else{
alert("如果长时间未收到短信，请重新点击获取验证码");
addCookie("secondsremained",60,60);//添加cookie记录,有效时间60s
settime($("#getvertification"));//开始倒计时应添加到上述else中，仅为调试方便
}
}
});

}
//开始倒计时
var countdown;
var t;
function settime(obj) { 
    countdown=getCookieValue("secondsremained");
 //   alert("countdown"+countdown);
    if (countdown == 1) { 
        obj.removeAttr('disabled');    
        obj.val("获取验证码"); 
        clearInterval(t);
        return;
    } else { 
        obj.attr('disabled', true); 
        obj.val("重新发送(" + countdown + ")"); 
        countdown--;
        editCookie("secondsremained",countdown,countdown+1);
    } 
    setTimeout(function() { settime(obj) },1000) //每1000毫秒执行一次
} 
  onload=function()
{
 phonenumber = document.getElementById("phonenumber");//js形式获得bieshehuile````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````			```````
 vertification = document.getElementById("vertification");   
}
  //  var jmz = {};
//	jmz.GetLength = function(str) {
  //  return str.replace(/[\u0391-\uFFE5]/g,"aa").length;
//	}; 
        $(document).ready(function(){  //页面加载完成即执行
        $("#getvertification").click(function(){//获取验证码响应JQuery
         onload();//加载phonenumber,vertification
         //检验手机号是否违法
        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; //正则表达式
		if(!myreg.test(phonenumber.value)) //不匹配的情况
		{ 
		  	 alert("请输入有效的手机号码！"); 
		   	return false; 
		} 
         sendCode(); // ajax发送数据  
});     //验证按钮响应        
        $("#vertify").click(function(){//
         onload();		//加载函数
    		if(vertification.value==null||vertification.value==""){
    			alert("请输入验证码");  //验证码不能为空
    			return;
    		}
         $.ajax({  //提交ajax数据
type: "POST",
url:"vertifyfindpasswordback.action",	//把表单数据发送到ajax.jsp
dataType: "json", 
data:{
"phonenumber":phonenumber.value,
"vertification":$("input[name='vertification']").val()//JQuery形式
},
async: true,
error: function(request) {
alert("发送请求失败！");
},
success: function(data) {

var json = eval("(" + data + ")"); //获得json数据返回
if(json.code=="true"){ //判断是否验证成功
document.form.submit();
}else if(json.code=="noexist"){
	alert("账户名不存在，手机号就是您的账户名");//账户名不存在，手机号就是您的账户名
}
else{
	alert("验证码错误，请重新输入");//验证错误
}
}
});       
});   
//用于上次计时未结束时
var v = getCookieValue("secondsremained");//获取cookie值
if(v!="NaN"&&v!="undefined"){
settime($("#getvertification"));//上次刷新之前处于倒计时状态，则继续倒计时
}
});   
  </script >
      </head>
  <body>
   <form action="jumptomain.action" name="form">
    手机号：
<input type="text" name="phonenumber" id="phonenumber" value="15763949907"onkeydown="if(event.keyCode==13) {document.getElementById('getvertification').click();}"></input>
<input type="button" value="获取验证码" id="getvertification" ></input>

<br>
验证码：
<input type="password" name="vertification" id="vertification" onkeydown="if(event.keyCode==13) {document.getElementById('vertify').click();}"></input><br>
<input type="button" value="确认" id="vertify"></input><br>

 </form>
  </body>
</html>
